package com.campus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {
    @Bean("ExecutorService")
    public ExecutorService getExecutorService() {
        //核心线程池数量
        int corePoolSize=5;
            //最大线程池数量
        int maxMumPoolSize=10;
        //非核心线程池最大空闲时间
        long keepAliveTime=1;
        //非核心线程池单位
        TimeUnit minutes = TimeUnit.MINUTES;
        //阻塞队列
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(15);
        //线程池工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //异常处理策略
        RejectedExecutionHandler handle = new ThreadPoolExecutor.AbortPolicy();
        //手动构造线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxMumPoolSize, keepAliveTime, minutes, workQueue, threadFactory, handle);
        //这个不推荐使用了
//        ExecutorService executorService = Executors.newFixedThreadPool(10);
        return threadPoolExecutor;
    }
}
